<?php

use yii\bootstrap\ActiveForm;
use common\helps\tools;

?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<style>
    .examine-content .input-group .form-control {
        margin-bottom: 20px;
        height: 30px;
        line-height: 30px;
    }

    .examine-content button {
        margin: 10px;
    }

    label {
        text-align: right;
    }

    .modal-body > form > div.col-md-12 {
        padding-bottom: 20px
    }
    .imgBox img{ height: 100px; margin-top: 10px; border: 1px solid #ddd; padding: 2px ; float: left}
    .imgBox div{ position: relative; float: left; margin-top: 30px; padding: 0 10px}
</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">
                    <div class="modal-body">
                        <?php $form = ActiveForm::begin(['id' => 'add_warehouse_form', 'action' => ['/stock/add-warehouse']]); ?>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">仓库名称</label>
                            <div class="col-sm-4">
                                <input name="id" id="id" type="hidden" value="<?= $warehouse ? $warehouse['id'] : '' ?>">
                                <input type="text" class="form-control" name="warehouseName" id="warehouseName" disabled="disabled"
                                       value="<?= $warehouse ? $warehouse['warehouseName'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">仓库编号</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="warehouseCode" id="warehouseCode" disabled="disabled"
                                       value="<?= $warehouse ? $warehouse['warehouseName'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">仓库属性</label>
                            <div class="col-sm-4">
                                <select id="storeType" name="storeType" class="form-control">
                                    <option value="">请选择</option>
                                    <option value="0" <?= ($warehouse && $warehouse['storeType'] == '0') ? 'selected' : '' ?>>常温仓</option>
                                    <option value="1" <?= ($warehouse && $warehouse['storeType'] == '1') ? 'selected' : '' ?>>冷冻仓</option>
                                    <option value="2" <?= ($warehouse && $warehouse['storeType'] == '2') ? 'selected' : '' ?>>虚拟仓</option>
                                </select>
                            </div>
                            <label class="col-sm-2 control-label">是否可用</label>
                            <div class="col-sm-4">
                                <select id="can_Frozen" name="can_Frozen" class="form-control">
                                    <option value="1" <?= ($warehouse && $warehouse['can_Frozen'] == '1') ? 'selected' : '' ?>>是</option>
                                    <option value="0" <?= ($warehouse && $warehouse['can_Frozen'] == '0') ? 'selected' : '' ?>>否</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">省</label>
                            <div class="col-sm-4">
                                <select id="province" name="province" class="form-control">
                                    <option value="">请选择</option>
                                    <?php foreach ($province as $value) { ?>
                                        <option value="<?= $value['ProvinceID'] ?>" <?= ($warehouse && $warehouse['province'] == $value['ProvinceName']) ? 'selected' : '' ?>><?= $value['ProvinceName'] ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                            <label class="col-sm-2 control-label">市</label>
                            <div class="col-sm-4">
                                <select id="city" name="city" class="form-control"></select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">区</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="area" id="area"
                                       value="<?= $warehouse ? $warehouse['area'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">地址</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="address" id="address"
                                       value="<?= $warehouse ? $warehouse['address'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">联系人</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="contacts" id="contacts"
                                       value="<?= $warehouse ? $warehouse['contacts'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">联系手机</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="mobile" id="mobile"
                                       value="<?= $warehouse ? $warehouse['mobile'] : '' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">联系电话</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="phone" id="phone"
                                       value="<?= $warehouse ? $warehouse['phone'] : '' ?>">
                            </div>
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-4">
                                <select id="useFlg" name="useFlg" class="form-control">
                                    <option value="1" <?= ($warehouse && $warehouse['useFlg'] == '1') ? 'selected' : '' ?>>启用</option>
                                    <option value="0" <?= ($warehouse && $warehouse['useFlg'] == '0') ? 'selected' : '' ?>>禁用</option>
                                </select>
                            </div>
                        </div>
                        <?php ActiveForm::end(); ?>
                    </div>
                    <div class="modal-footer" style="border: 0">
                        <button type="button" class="btn btn-default" onclick="history.back();">返回
                        </button>
                        <a type="button" class="btn btn-primary ajax_post_add_warehouse"> 提交 </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
    <script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
    <script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
    <script type="text/javascript" src="../static/plugins/jquery.form.js"></script>
    <script type="text/javascript" src="../static/plugins/layer/layer.js"></script>
    <!-- 自定义js -->

    <script>
        var city = <?=$city ?>;
        $(function () {
            $('#province').on('change', function () {
                changeProvince();
            });
            changeProvince('<?=$warehouse ? $warehouse['city'] : '' ?>');
        });

        function changeProvince(sname) {
            var cityArr = city[$('#province').val()];
            $('#city').empty();
            var html = '<option value="">请选择</option>';
            $.each(cityArr, function (i, v) {

                html += '<option value="' + v['CityID'] + '" ' + (sname == v['CityName'] ? 'selected' : '') + '>' + v['CityName'] + '</option>'
            });
            $('#city').append(html);
        }

        $(".ajax_post_add_warehouse").on('click', function () {

            var warehouseName = $('#warehouseName').val();
            var warehouseCode = $('#warehouseCode').val();
            var storeType = $('#storeType').val();
            var provinceID = $('#province').val();
            var cityID = $('#city').val();
            var area = $('#area').val();
            var address = $('#address').val();
            var contacts = $('#contacts').val();
            var mobile = $('#mobile').val();
            var phone = $('#phone').val();

            if (warehouseName == '') {
                $.jBox.error('请填写仓库名称', '错误');
                return false;
            }
            if (warehouseCode == '') {
                $.jBox.error('请填写仓库编号', '错误');
                return false;
            }
            if (storeType == '') {
                $.jBox.error('请选择仓库属性', '错误');
                return false;
            }
            if (provinceID == '') {
                $.jBox.error('请选择省份', '错误');
                return false;
            }
            if (cityID == '') {
                $.jBox.error('请选择城市', '错误');
                return false;
            }
            if (area == '') {
                $.jBox.error('请选择区域', '错误');
                return false;
            }
            var province = $('#province option:selected').text();
            var city = $('#city option:selected').text();
            var param = {
                id: $('#id').val(),
                warehouseName: warehouseName,
                warehouseCode: warehouseCode,
                storeType: storeType,
                can_Frozen: $('#can_Frozen').val(),
                province: province,
                city: city,
                area: area,
                address: address,
                contacts: contacts,
                mobile: mobile,
                phone: phone,
                useFlg: $('#useFlg').val()
            };
            $.post(
                $('#add_warehouse_form').attr('action'),
                param,
                function (data) {
                    if (data.code == 200) {
                        $.jBox.tip('处理成功', 'success');
                        window.setTimeout(function () {
                            history.back();
                        }, 1000);
                    } else {
                        $.jBox.error(data.val, '错误');
                    }
                },
                'json'
            );

        });

    </script>